<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 任务管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择任务！','error');
	}
}
$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['levels']['0']=array('id'=>0,'title'=>'默认等级');
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	} 
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_renwushop_renwu',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['url']=custom_url('jy_renwushop/renwu.detail//id='.$item['id'],true);
			$item['endstatus']=$item['starttime']<time() ? $item['endtime']>time()? 1 : 2 : 0;
			$item['statustitle']=$item['starttime']<time() ? $item['endtime']>time()? '去拼团' : '已结束' : '未开始';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'任务列表'),
			array('op'=>'edit','title'=>'添加任务'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]:</br>1、不单独设置任务信息的任务，将默认：设置--基本设置--里面默认设置！','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'任务名称','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'member','title'=>'发布者_Member','type'=>'member'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'fee','title'=>'任务奖励','type'=>'string'),
			array('name'=>'hours','title'=>'任务时间/小时','type'=>'string'),
			array('name'=>'totalnum','title'=>'总共数量','type'=>'string'),
			array('name'=>'lastnum','title'=>'剩余数量','type'=>'string'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
		),'action'=>array(
			array('type'=>'post','op'=>'isdel','title'=>'同意删除并退款','show'=>array('isdel'=>1)),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'任务名称','type'=>'string','width'=>'20'),
			array('name'=>'status','title'=>'是否启用','type'=>'select','options'=>'status','width'=>'20'),
		)
	);
	$return['status']=array(array('id'=>'0','title'=>'禁用'),array('id'=>'1','title'=>'启用'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_renwushop_renwu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['steps']=$item['steps']?json_decode($item['steps'],true):array();
	}else{
		$item=array('steps'=>array());
	}
	$item['pcd']=$item['pcd']?json_decode($item['pcd'],true):array('city'=>'');
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回任务列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('type'=>'radio','title'=>'任务种类','name'=>'type','options'=>'renwu_types'),
								array('name'=>'ad_image','title'=>'广告图片','type'=>'image','show'=>array('type'=>0)),
								array('name'=>'ad_video','title'=>'广告视频','type'=>'video','show'=>array('type'=>1)),
								array('type'=>'string','title'=>'推广链接','name'=>'tuiurl','help'=>'比如拼多多商品链接，抖音链接等'),
								array('name'=>'pcd','title'=>'任务区域','type'=>'pcd'),
								array('name'=>'title','title'=>'任务名称','type'=>'string'),
								array('name'=>'desc','title'=>'任务简介','type'=>'text'),
								array('name'=>'image','title'=>'图片','type'=>'image'),
								array('name'=>'groupid','title'=>'所属分类','type'=>'select','options'=>'groups'),
								array('name'=>'mid','title'=>'发布者','type'=>'choose','action'=>'core/member.get_member/'),
								array('type'=>'unitstring','title'=>'奖励金额','name'=>'fee','unit'=>'元'),
								array('type'=>'radio','title'=>'做单次数','name'=>'times_type','options'=>'times_type'),
								array('type'=>'radio','title'=>'任务时长','name'=>'hours_type','options'=>'hours_type'),
								array('type'=>'unitstring','title'=>'任务总量','name'=>'totalnum','unit'=>'次','help'=>''),
								array('type'=>'unitstring','title'=>'审核时间','name'=>'shenhedays','unit'=>'天'),
							)
						),
						array(
							'title'=>'任务步骤',
								'list'=>array(
									array(
										'type'=>'array',
										'name'=>'steps',
										'add_tip'=>'添加一个步骤',
											'list'=>array(
												array('type'=>'radio','name'=>'type','options'=>'step_type','title'=>'步骤类型'),
												array('type'=>'string','name'=>'url','title'=>'文字'),
												array('type'=>'image','name'=>'picurl','title'=>'图片'),
											),
											'details'=>array(
												array('type'=>'radio','name'=>'type','options'=>'step_type','title'=>'步骤类型'),
												array('type'=>'title','title'=>'链接推广模式，这里无需设置任何步骤提示；前端会展示（基础信息）里面设置的链接内容；','show'=>array('type'=>0)),
												array('type'=>'string','name'=>'url','show'=>array('type'=>1),'title'=>'文字','help'=>'比如：第一步：扫图中二维码'),
												array('type'=>'image','name'=>'picurl','show'=>array('type'=>1),'title'=>'图片'),				
											)
									)
								)
						),
			)
	);
	$return['groups']=$this->getall('group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['step_type']=$this->step_type;
	$return['gift_type']=$this->gift_type;
	$return['credit_type']=$this->credit_type;
	$return['renwu_types']=$this->renwu_types;
	$return['hours_type']=$this->hours_type;
	$return['times_type']=$this->times_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['totalnum'] || $postdata['totalnum']<2){
		exi('任务总量必须大于等于2','error');
	}
	if($postdata['fee']<=0){
		exi('任务奖励必填','error');
	}
	if(!$postdata['title']&& !$postdata['image']){
		exi('请设置任务名称,任务图片！','error');
	}
	$data=array(
		'type'=>intval($postdata['type']),
		'title'=>trim($postdata['title']),
		'desc'=>trim($postdata['desc']),
		'image'=>trim($postdata['image']),
		'pcd'=>ijson_encode($postdata['pcd']),
		'steps'=>ijson_encode($postdata['steps']),
		'mid'=>intval($postdata['mid']),
		'goodsid'=>intval($postdata['goodsid']),
		'groupid'=>intval($postdata['groupid']),
		'hours_type'=>intval($postdata['hours_type']),
		'times_type'=>intval($postdata['times_type']),
		'totalnum'=>intval($postdata['totalnum']),
		'fee'=>trim($postdata['fee']),
		'ad_image'=>trim($postdata['ad_image']),
		'ad_video'=>trim($postdata['ad_video']),
		'helpurl'=>intval($postdata['helpurl']),
		'tuiurl'=>trim($postdata['tuiurl']),
		'shenhedays'=>intval($postdata['shenhedays']),
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),	
		'createtime'=>TIMESTAMP,
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_renwushop_renwu',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_renwushop_renwu',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$isnum=pdo_count('jy_renwushop_renwuorder',array('uniacid'=>$_SESSION['uniacid'],'renwuid'=>$id),'id');
			if($isnum){
				exi('有人参与了，不能删除！','error');
			}
			pdo_delete('jy_renwushop_renwu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='isdel'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要处理的任务！','error');
	}
	$this->model('renwu')->refund_pay($id);
	
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的任务！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_renwushop_renwu',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
